package cn.zijian.wechat;

import android.support.design.widget.TabLayout;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import cn.zijian.wechat.adapter.PagerAdapter;

public class MainActivity extends AppCompatActivity
{
    private TabLayout tabLayout = null;
    private PagerAdapter pagerAdapter = null;
    private ViewPager viewPager = null;

    private TabLayout.Tab messageTab = null;
    private TabLayout.Tab contactTab = null;
    private TabLayout.Tab profileTab = null;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //隐藏自带的bar
//        getActionBar().hide();
        setContentView(R.layout.activity_main);

        initPageViews();
    }

    private void initPageViews()
    {
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        //将TabLayout与ViewPager绑定在一起
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

        //指定Tab的位置
        messageTab = tabLayout.getTabAt(0);
        contactTab = tabLayout.getTabAt(1);
        profileTab = tabLayout.getTabAt(2);

        setTabIcons();
        listenTabItemClick();
    }

    /**
     * 监听tab点击事件
     */
    private void listenTabItemClick()
    {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
        {
            @Override
            public void onTabSelected(TabLayout.Tab tab)
            {
                int tabItemCount = tabLayout.getTabCount();
                for(int i = 0; i< tabItemCount; i++)
                {
                    if(i == tab.getPosition())
                    {
                        tabLayout.getTabAt(i).setIcon(PagerAdapter.tabItems[i].getIconActive());
                    }
                    else
                    {
                        tabLayout.getTabAt(i).setIcon(PagerAdapter.tabItems[i].getIcon());
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab)
            {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab)
            {

            }
        });
    }


    //设置Tab的图标
    private void setTabIcons()
    {
        messageTab.setIcon(R.drawable.message_pressed);
        contactTab.setIcon(R.drawable.contact_normal);
        profileTab.setIcon(R.drawable.profile_normal);
    }

}
